import axios from 'axios'
import { Toast } from 'antd-mobile';
import { getHost } from '@/utils/host'

export const HOST = getHost()

axios.defaults.timeout = 10000
axios.defaults.headers = { 'Content-Type': 'application/json;charset=UTF-8' }
axios.defaults.baseURL = HOST

// 请求拦截器
axios.interceptors.request.use(
  config => {
    Toast.loading('加载中...', 0)
    return config
  },
  err => {
    Toast.info(JSON.stringify(err))
    return Promise.resolve(err)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    Toast.hide()
    const { code, status } = response.data
    return response
  },
  err => {
    Toast.info('网络错误')
    return new Promise(() => { })
  }
)

export const get= (url, params={})=>{
    return new Promise((resolve, reject) => {
        axios
          .get(url, params)
          .then(res =>{
            return res.status === 200 ? resolve(res.data) : reject(res.data)
          })
          .catch(err => {
            reject(err)
          })
      })
}

export const post = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(res => {
        return res.status === 200 ? resolve(res) : reject(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}


